package cn.com.headfree.util;

import cn.com.headfree.base.common.MyConstant;
import cn.com.headfree.depterprela.service.IDeptErpRelaService;
import cn.com.headfree.depterprela.service.OrgDeptService;
import cn.com.headfree.entity.TDeptEmpRela;
import cn.com.headfree.entity.TOrgDept;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.XWPFTemplate;
import com.gta.edu.sdk.util.EhCacheUtil;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 根据erp_no获取部门全路径工具类
 */
@Component
public class GetFullDeptByErpNoUtil {

    private static final Logger LOG = LogManager.getLogger(GetFullDeptByErpNoUtil.class);

    private static int dictionaryTime;
    @Value("${redis.dictionary.timelong}")
    public void setDictionaryTime(int dictionaryTime) {
        this.dictionaryTime = dictionaryTime;
    }

    @Autowired
    private IDeptErpRelaService deptSevice;

    private static IDeptErpRelaService deptErpRelaService;

    @Autowired
    private OrgDeptService orgSevice;

    private static OrgDeptService orgDeptService;


    @PostConstruct
    public void init() {
        deptErpRelaService = deptSevice;
        orgDeptService = orgSevice;
    }

    private GetFullDeptByErpNoUtil(){}

    /**
     * 根据个人的erp_no获取部门全路径
     */
    public static String getFullDept(Map<String,Object> map,String erpNo){
        List<TDeptEmpRela> deptEmpRelas = deptErpRelaService.getFullDeptPathByErpNo(erpNo);
        List<String> deptFullPaths = new ArrayList<>();
        if(deptEmpRelas != null && deptEmpRelas.size() > 0){
            String path = deptEmpRelas.get(0).getPath();
            String [] deptIds = path.split("[|]");
            if(deptIds != null && deptIds.length > 0){
                if(map != null){
                    for(int i = 1;i<deptIds.length;i++){
                        deptFullPaths.add((String)map.get(deptIds[i]));
                    }
                }
            }
        }
        if(deptFullPaths != null && deptFullPaths.size() > 0){
            String path = StringUtils.join(deptFullPaths,"-");
            return path;
        }else {
            return null;
        }
    }


    public static Map<String,Object> getOrgDeptList(){
        List<TOrgDept> orgDepts = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        String orgDeptCache = EhCacheUtil.getValue("org-dept-info-");
        String orgDeptRedis = RedisClusterUtil.getInstance().getString("org-dept-info-");
        if (!StringUtil.isNullOrEmpty(orgDeptCache)) {
            JSONArray arr = JSONObject.parseArray(orgDeptCache);
            orgDepts = arr.toJavaList(TOrgDept.class);
            if(StringUtil.isNullOrEmpty(orgDeptCache)){
                RedisClusterUtil.getInstance().setString("org-dept-info-", JSONObject.toJSONString(orgDepts), dictionaryTime);
            }
        }else{
            if(!StringUtil.isNullOrEmpty(orgDeptCache)){
                orgDepts = (List<TOrgDept>) JSONObject.parseObject(orgDeptRedis);
                EhCacheUtil.put("org-dept-info-", JSONObject.toJSONString(orgDepts));
            }else{
                orgDepts = orgDeptService.findAll();
                EhCacheUtil.put("org-dept-info-", JSONObject.toJSONString(orgDepts));
                RedisClusterUtil.getInstance().setString("org-dept-info-", JSONObject.toJSONString(orgDepts), dictionaryTime);
            }
        }
        if(orgDepts != null && orgDepts.size() > 0){
            for(TOrgDept dept:orgDepts){
                map.put(dept.getId(),dept.getName());
            }
            return map;
        }else{
            return null;
        }
    }


}
